define([
        "jquery",
        "IO",
        "UPLOADUI",
        "UPLOAD",
        "jqueryUI",
        "bootbox"
    ], function ($, IO, upui, up, jui, bootbox) {
        $(function () {
            var train_id = $("#train_id_hidden").val();
            var which_btn;
            var report_id;
            gettable();
            function gettable() {
                var grid;
                //每列数据列性
                var columns = [
                    {
                        title: '编号',
                        dataIndex: 'train_result_id',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            return "<span>" + data[datarow].train_result_id + "</span>";
                        }
                    },
                    {
                        title: '人员姓名',
                        dataIndex: 'user_name',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            return "<span>" + data[datarow].user.user_name + "</span>";
                        }
                    },
                    {
                        title: '成绩',
                        dataIndex: 'study_result',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            return "<span>" + data[datarow].study_result + "</span>";
                        }
                    },
                    {
                        title: '报告发送时间',
                        dataIndex: 'timestamp',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            var date = new Date(data[datarow].timestamp);
                            create_year = date.getFullYear();
                            create_month = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
                            create_day = date.getDate() < 10 ? '0'+date.getDate() : date.getDate() ;
                            create_hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() ;
                            create_min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() ;
                            create_sec = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
                            return "<span>"+create_year+'-'+create_month+'-'+create_day+' '+create_hours+':'+create_min+':'+create_sec+ ' '+"</span>";
                        }
                    },
                    {
                        title: '操作',
                        dataIndex: 'action',
                        dataSource: function (data, datarow, gridobj, current_column) {
                            return "<span><a href='#' class='update' style='color: green;' report_id='"+data[datarow].train_result_id+"'>修改</a></span>&nbsp;&nbsp;&nbsp;<span><a href='#' class='delete' style='color: red;' report_id='"+data[datarow].train_result_id+"'>删除</a></span>";
                        }
                    },

                ];


                //数组初始化
                function dataInit() {
                    grid = new Grid("/reportList", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);
                    //初始化
                    grid.Init();
                }

                //获取输入框内容
                function get_input_param() {
                    var user_name = $("#user_name").val();

                    var params = "user_name=" + user_name +"&train_id=" + train_id;

                    return params;
                }
                dataInit();
                $("#btnSearch").on('click',function(){
                    dataInit();
                })

            }

            //列表点击删除按钮
            $(document).on('click','.delete',function () {
                report_id=$(this).attr("report_id");//报告id
                if(report_id){
                    bootbox.confirm("确定要删除该报告?",function(result){
                        if(result) {
                            IO.get("/ct/act/report_delete",{report_id:report_id},function(d){
                                if(d.code =='0'){
                                    bootbox.alert(d.msg,function(){
                                        gettable();
                                    })}
                            });
                        }
                    });
                }
            })

            //模态框隐藏
            $('#myModal').on('hidden.bs.modal', function () {
                $("#ReportName").val("-1");
                editor.html("");
                $("#ReportScore").val("");
            });
            //新增报告按钮
            $('#btnAddReport').on('click',function () {
                $('#myModal').modal('show');
                which_btn = 0;
            })
            //获取报告详情
            $(document).on('click',".update",function(){
                report_id = $(this).attr("report_id");
                IO.post("/ct/act/report_detail",{report_id:report_id},function(d){
                    if(d.code==0){
                        $("#ReportName").val(d.data.user_id);
                        editor.html(d.data.study_report);
                        $("#ReportScore").val(d.data.study_result);

                        $('#myModal').modal('show');
                        which_btn = 1;
                    }else{
                        bootbox.alert(d.msg);
                    }
                })
            })
            //点击发送按钮
            $('#btnAddReportSure').on('click',function () {
                var  params={};
                var user_id     = $("#ReportName").val();//用户姓名
                var report_body = "<html><head><style>img {width: 100%;}</style></head><body>"+editor.html()+"</body></html>";
                var result      = $("#ReportScore").val();

                if(user_id == "-1"){
                   bootbox.alert("请选择用户");
                   return;
                }

                var reg = new RegExp("^[0-9]*$");
                if(!reg.test(result) || result == ""){
                    bootbox.alert("成绩格式不对");
                    return;
                }

                if(report_body == ""){
                   bootbox.alert("报告内容不能为空");
                   return;
                }
                params = {user_id:user_id,train_id:train_id,report_body:report_body,result:result};

                //判断是"增加"还是"修改"
                if(which_btn == 0){
                    IO.post("/ct/act/report_add",params,function(d){
                        if(d.code=="0") {
                            bootbox.alert(d.msg,function(){
                                $("#myModal").modal('hide');
                                gettable();
                            });
                        }else{
                            $("#myModal").modal('hide');
                            bootbox.alert(d.msg);
                        }
                    });
                }
                if(which_btn == 1){
                    params.report_id = report_id;
                    IO.post("/ct/act/report_editor",params,function(d){
                        if(d.code=="0") {
                            bootbox.alert(d.msg,function(){
                                $("#myModal").modal('hide');
                                gettable();
                            });
                        }else{
                            $("#myModal").modal('hide');
                            bootbox.alert(d.msg);
                        }
                    });
                }

            })




        })
    }

)
